package com.ysd.lis.listener;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.ysd.lis.entity.ReadExcelData;
import com.ysd.lis.request.Resp;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 此类不能被Spring管理
 *
 * @program: server_lis
 * @author: liuQi
 * @create: 2024-03-06 08:48
 */
@Slf4j
public class ReadExcelDataListener extends AnalysisEventListener<ReadExcelData> {
    public List<ReadExcelData> importList = new ArrayList<>();
    public Map<Integer, String> headMap = null;
    public static final ThreadLocal<Resp> RESP = new ThreadLocal<>();
    @Override
    public void invoke(ReadExcelData data, AnalysisContext context) {
        importList.add(data);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        Resp resp = new Resp();
        resp.setImportList(importList);
        resp.setHeadMap(headMap);
        RESP.set(resp);
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        Map<Integer, String> filteredMap = headMap.entrySet()
                .stream()
                .filter(e -> e.getKey() >= 4 && StrUtil.isNotEmpty(e.getValue()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        this.headMap = filteredMap;
    }
}
